<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class ProfileForm extends CFormModel {

    public $url;
    public $imgX;
    public $imgY;
    public $imgWidth;
    public $imgHeight;
//    private $_identity;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('url,imgX,imgY,imgWidth,imgHeight', 'required'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'photo' => '头像名称'
        );
    }
    
    public function createImg($img, $radio) {
        $x = $this->imgX;
        $y = $this->imgY;
        $olgWidth = $this->imgWidth;
        $oldHeight = $this->imgHeight;
        $newWidth = $olgWidth * $radio;
        $newHeight = $oldHeight * $radio;
        $image = imagecreatefromjpeg($img);
        $thumb = imagecreatetruecolor ($newWidth, $newHeight); 
        imagecopyresized ($thumb, $image, 0, 0, $x, $y, $newWidth, $newHeight, $olgWidth, $oldHeight); 
        $userid = Yii::app()->user->id;
		$ext = pathinfo($img)['extension'];
        $path = "uploadfile/". $userid. "/". $radio. ".".$ext;
        imagejpeg($thumb, $path); 

        imagedestroy($thumb);
        imagedestroy($image);
    }

}
